/**
 * 自定义公共函数
 */
export default {
	/**
	 * 添加商品到购物车
	 * let addCartRes = vk.myfn.cart.add({ goodsInfo, selectShop });
	 */
	add(obj) {
		let vk = uni.vk;
		let { goodsInfo = {}, selectShop = {} } = obj;
		let res = { code: 0, msg: "" };
		let goods = {
			sku_id: selectShop._id, // 主要参数 - skuID
			buy_num: Number(selectShop.buy_num), // 主要参数 - 购买数量
			sku_name: selectShop.sku_name, // 展示参数 - sku名称
			image: selectShop.image, // 展示参数 - sku图片
			price: Number(selectShop.price), // 展示参数 - sku价格
			stock: Number(selectShop.stock), // 展示参数 - sku库存
			goods_id: goodsInfo._id, // 展示参数 - 商品ID
			goods_name: goodsInfo.name, // 展示参数 - 商品名称
			goods_type: goodsInfo.type, // 展示参数 - 商品类型
			cart_rule: goodsInfo.cart_rule || {}, // 展示参数 - 购物车规则
			action_checked: true, // 辅助参数 - 购物车是否选中
		};

		if (!goods.goods_id || !goods.sku_id) {
			let errMsg = "商品信息错误";
			console.error(errMsg);
			return { code: -1, msg: errMsg };
		}
		let list = vk.myfn.cart.getList("list");
		let isNotExistent = true;
		for (let i in list) {
			if (list[i].goods_id === goods.goods_id && list[i].sku_id === goods.sku_id) {
				list[i].buy_num += goods.buy_num;
				list[i].stock = goods.stock;
				if (list[i].buy_num > goods.stock) {
					list[i].buy_num = goods.stock;
				}
				isNotExistent = false;
				break;
			}
		}
		if (isNotExistent) {
			// 如果不存在，代表新增商品，记录新增时间
			goods.add_time = Date.now();
			list.push(goods);
		}
		vk.vuex.dispatch('$cart/setCart', list);
		let nameStr = `${goods.goods_name} ${goods.sku_name}`;
		if (nameStr.length > 20) {
			nameStr = nameStr.substring(0, 20) + "...";
		}
		res.msg = `已将（${nameStr}）加入购物车`;
		let count = String(vk.myfn.cart.getTotal("list"));
		uni.setTabBarBadge({
			index: 2,
			text: count
		});
		return res;
	},
	/**
	 * 请求云端获取最新的商品信息
	 * let news_goods_sku_list = vk.myfn.cart.getSkuData(goods_sku_list);
	 */
	async getSkuData(goods_sku_list){
		let vk = uni.vk;
		if (vk.pubfn.isNotNull(goods_sku_list)) {
			//goods_sku_list = vk.pubfn.copyObject(goods_sku_list);
			let sku_ids = goods_sku_list.map((item, index) => {
				return item.sku_id;
			});
			let data = await vk.callFunction({
				url: 'client/pub.getGoodsSkuList',
				data: {
					sku_ids
				}
			});
			if (vk.pubfn.isNotNull(data.list)) {
				goods_sku_list.map((item, index) => {
					let skuInfo = vk.pubfn.getListItem(data.list, "_id", item.sku_id);
					if (skuInfo) {
						item.goods_name = skuInfo.goodsInfo.name;
						item.goods_type = skuInfo.goodsInfo.type;
						item.cart_rule = skuInfo.goodsInfo.cart_rule;
						item.image = skuInfo.image;
						item.price = skuInfo.price;
						item.sku_name = skuInfo.sku_name;
						item.stock = skuInfo.stock;
					}
				});
			}
			return goods_sku_list;
		}
	},
	/**
	 * 添加商品到临时购物车
	 * let addTempCartRes = vk.myfn.cart.addTemp(goods_sku_list);
	 */
	async addTemp(goods_sku_list) {
		let vk = uni.vk;
		let res = { code: 0, msg: "" };
		let list = [];
		goods_sku_list.map((item, index) => {
			list.push({
				sku_id: item.sku_id, // 主要参数 - skuID
				buy_num: item.buy_num, // 主要参数 - 购买数量
				sku_name: item.sku_name, // 展示参数 - sku名称
				image: item.image, // 展示参数 - sku图片
				price: item.price, // 展示参数 - sku价格
				stock: item.stock, // 展示参数 - sku库存
				goods_id: item.goods_id, // 展示参数 - 商品ID
				goods_name: item.goods_name, // 展示参数 - 商品名称
				goods_type: item.goods_type || 0, // 展示参数 - 商品类型
				cart_rule: item.cart_rule || {}, // 展示参数 - 购物车规则
				action_checked: true, // 辅助参数 - 购物车是否选中
			});
		});
		// 存入临时购物车
		vk.setVuex('$cart.temp', list);
		res.list = list;
		return res;
	},
	/**
	 * 获取购物车商品列表
	 * let list = vk.myfn.cart.getList(key);
	 */
	getList(key="list"){
		let vk = uni.vk;
		return vk.getVuex('$cart.'+key) || [];
	},
	/**
	 * 获取购物车总商品数量
	 * let total = vk.myfn.cart.getTotal(key);
	 */
	getTotal(key="list"){
		let vk = uni.vk;
		let list = vk.myfn.cart.getList(key);
		let total = 0;
		for (let i in list) {
			total += list[i].buy_num;
		}
		return total;
	},
	/**
	 * 获取购物车总商品数量(已勾选的)
	 * let total = vk.myfn.cart.getBuyNumTotal(key);
	 */
	getBuyNumTotal(key="list"){
		let vk = uni.vk;
		let list = vk.myfn.cart.getList(key);
		let total = 0;
		list.map(item => {
			if (item.action_checked) {
				total += item.buy_num;
			}
		});
		return total;
	},
	/**
	 * 获取购物车商品总价
	 * let total = vk.myfn.cart.getBuyPriceTotal(key);
	 */
	getBuyPriceTotal(key="list"){
		let vk = uni.vk;
		let list = vk.myfn.cart.getList(key);
		let moneyTotal = 0;
		list.map(item => {
			if (item.action_checked) {
				moneyTotal += item.buy_num * item.price;
			}
		});
		return (moneyTotal / 100).toFixed(2);
	},
};
